package com.lancoo.klgcourseware.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.bean.KlgConstant;
import com.lancoo.klgcourseware.entity.bean.SpellCharacterBean;
import com.lancoo.klgcourseware.utils.KlgToolUtils;

import java.util.List;


/**
 * Created by pengmi on 2019/1/3.
 * Description:
 */

public class FillSpellCharacterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<SpellCharacterBean> spellCharacterBeans;
    private boolean isTraining, isBigSize,isAlert;
    private int trainIndex=-1,totalIndex=5;

    public FillSpellCharacterAdapter(Context mContext, List<SpellCharacterBean> spellCharacterBeans) {
        this.mContext = mContext;
        this.spellCharacterBeans = spellCharacterBeans;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.klg_list_item_spell_character, viewGroup, false);
        SpellCharacterVh spellCharacterVh=new SpellCharacterVh(inflate);
        return spellCharacterVh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        SpellCharacterVh spellCharacterVh= (SpellCharacterVh) viewHolder;
        spellCharacterVh.initData(spellCharacterBeans.get(i),i);
    }

    @Override
    public int getItemCount() {
        return spellCharacterBeans.size();
    }


    private class SpellCharacterVh extends RecyclerView.ViewHolder{
        public TextView tv_character,tv_point;
        public View underLine,parent;

        public SpellCharacterVh(@NonNull View itemView) {
            super(itemView);
            tv_character=itemView.findViewById(R.id.tv_character);
            tv_point=itemView.findViewById(R.id.tv_point);
            underLine=itemView.findViewById(R.id.underline);
            parent=itemView.findViewById(R.id.layout_input_char);
        }

        public void initData(SpellCharacterBean spellCharacterBean,int position){
            tv_character.setText(spellCharacterBean.getCharacter());
            tv_point.setVisibility(spellCharacterBean.isHasPoint()||spellCharacterBean.isHasLine()?View.VISIBLE:View.GONE);
            tv_point.setText(spellCharacterBean.isHasLine()?"-":"·");
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv_character.getLayoutParams();
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) underLine.getLayoutParams();
            int dp2=KlgToolUtils.dip2px(mContext,2);
            parent.setPadding(dp2,0,dp2,0);
            if(isTraining){
                layoutParams.rightMargin=dp2;
                lp.addRule(RelativeLayout.ALIGN_LEFT,R.id.tv_character);
                tv_character.setVisibility(spellCharacterBean.isHideWord()?View.INVISIBLE:View.VISIBLE);
                tv_character.setMinWidth(KlgToolUtils.dip2px(mContext,spellCharacterBean.isHideWord()?18:0));
                underLine.setVisibility(!spellCharacterBean.isHideWord()?View.INVISIBLE:View.VISIBLE);
                if(trainIndex==-2){
                    if(spellCharacterBean.isHasSpecialChar()){
                        tv_character.setText(spellCharacterBean.getCharacter()+spellCharacterBean.getSpecialChar());
                    }
                    layoutParams.rightMargin=0;
                    if(spellCharacterBean.isHasPoint()){
                        lp.addRule(RelativeLayout.ALIGN_LEFT,R.id.tv_point);
                    }else{
                        lp.addRule(RelativeLayout.ALIGN_LEFT,R.id.tv_character);
                    }
                    parent.setPadding(0,0,0,0);
                    tv_character.setVisibility(View.VISIBLE);
                    underLine.setVisibility(View.VISIBLE);
//                    tv_point.setVisibility(View.GONE);
                    tv_character.setMinWidth(0);
                    tv_point.setTextColor(Color.parseColor("#004ce8"));
                    tv_character.setTextColor(Color.parseColor("#004ce8"));
                    underLine.setBackgroundColor(Color.parseColor("#004ce8"));
                }else if(position<=trainIndex){
                    tv_point.setTextColor(Color.parseColor("#004ce8"));
                    tv_character.setTextColor(Color.parseColor("#004ce8"));
                    underLine.setBackgroundColor(Color.parseColor("#004ce8"));
                }else{
                    tv_character.setTextColor(Color.parseColor("#aae3f5"));
                    tv_point.setTextColor(Color.parseColor("#aae3f5"));
                    underLine.setBackgroundColor(Color.parseColor("#aae3f5"));
                }

                if(isAlert){
                    tv_character.setTextColor(Color.parseColor("#b9b9b9"));
                    tv_point.setTextColor(Color.parseColor("#b9b9b9"));
                    underLine.setBackgroundColor(Color.parseColor("#b9b9b9"));
                    tv_character.setTextSize(KlgToolUtils.isPad(mContext)?24:20);
                    tv_point.setTextSize(KlgToolUtils.isPad(mContext)?24:20);
                }

            }else{
                layoutParams.rightMargin=dp2;
                lp.addRule(RelativeLayout.ALIGN_LEFT,R.id.tv_character);
                tv_character.setMinWidth(KlgToolUtils.dip2px(mContext,0));
                tv_character.setTextSize(isBigSize?(KlgToolUtils.isPad(mContext)?36:30):(KlgToolUtils.isPad(mContext)?24:20));
                tv_point.setTextSize(isBigSize?(KlgToolUtils.isPad(mContext)?36:30):(KlgToolUtils.isPad(mContext)?24:20));
                underLine.setBackgroundColor(Color.parseColor(spellCharacterBean.getScores()>= KlgConstant.SCORE_QUALITY||!spellCharacterBean.isHideWord()?"#004ce8":"#ff0000"));
                tv_character.setTextColor(Color.parseColor(spellCharacterBean.getScores()>=KlgConstant.SCORE_QUALITY||!spellCharacterBean.isHideWord()?"#004ce8":"#ff0000"));
                tv_point.setTextColor(Color.parseColor("#004ce8"));
                underLine.setVisibility("·".equalsIgnoreCase(spellCharacterBean.getCharacter())||!spellCharacterBean.isHideWord()?View.GONE:View.VISIBLE);
                tv_character.setVisibility(spellCharacterBean.isHideWord()?View.VISIBLE:View.VISIBLE);
                if(spellCharacterBean.isHasSpecialChar()){
                    tv_character.setText(spellCharacterBean.getCharacter()+spellCharacterBean.getSpecialChar());
                }
            }

        }

    }

    public void changeRepeat(int trainIndex,int totalIndex){
        updateTrainIndex(trainIndex,totalIndex);
        notifyDataSetChanged();
    }

    public void updateTrainIndex(int trainIndex,int totalIndex){
        this.trainIndex=trainIndex;
        this.totalIndex=totalIndex;
    }


    public void setTrainModel(boolean isTraining,boolean isBigSize){
        this.isTraining=isTraining;
        this.isBigSize =isBigSize;
        trainIndex=-1;
        notifyDataSetChanged();
    }
    public void setAlert(boolean isAlert){
        this.isAlert=isAlert;
    }
}
